﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Newtonsoft.Json.Linq;


public partial class double_danshicopy : System.Web.UI.Page
{
    protected Model.LotteryQs lotteryqs = null;
    protected decimal winmonenyTotal = 0;
    protected List<Model.LotteryDes> problemList = null;
    protected List<Model.NewsSite> newsList = null;
    protected List<Model.UsersAccount> userAccount = null;
    protected decimal jiangjin2 = 0;
    string duzhi = "";
    public string zifucuan = "";
    public string InputData = "";
    public string strtext = "";
    public string bbb = "";
    public int note = 0;
    public string br = "";
    public string str = "ssssssss";
    static int zhs = 0;
    protected List<Model.LotteryAnnouncement> lotteryQS = null;
    protected int type = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        //string fenzhong = DateTime.Now.AddMinutes(-10).ToString();
        lotteryqs = BLL.LotteryQs.GetModel(1);
        int totalCount = 0;
        problemList = BLL.LotteryDes.GetPageList_Where(0, 4, 1, out totalCount);

        int newsCount = 0;
        newsList = BLL.NewsSite.GetPageList_Where(0, 20, 8, out newsCount);
        //用户账户
        int totalCount2 = 0;
        userAccount = BLL.UsersAccount.getList(0, int.MaxValue, out totalCount2);
        foreach (Model.UsersAccount user in userAccount)
        {
            winmonenyTotal += user.winTotal;
        }
        jiangjin2 = Convert.ToDecimal(winmonenyTotal)/100000000;
        int curCount = 0;
        lotteryQS = BLL.LotteryAnnouncement.GetPageList_Where(0, 2, type, out curCount);
    }

    public string SelectName(object Uid)
    {
        Model.CMember cm = BLL.CMember.GetModel(Convert.ToInt32(Uid));
        if (cm != null)
        {
            return cm.nickname; //用户真实姓名
        }
        else
        {
            return "***";
        }
    }

 
    private void ExistsFile(string FilePath)
    {
      if (!File.Exists(FilePath))
        {
            FileStream fs = File.Create(FilePath);
            fs.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strWeek = DateTime.Now.DayOfWeek.ToString();
        DateTime strGetTime = Convert.ToDateTime(DateTime.Now.ToShortTimeString());
        DateTime strFixTime = Convert.ToDateTime("21:05");  //设置截止日期，9.05


        if (this.FileUpload1.HasFile)
        {

            bool fileIsValid = false;
            #region 原来
            //   如果确认了上传文件，则判断文件类型是否符合要求
            if (this.FileUpload1.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
                String restrictExtension = ".txt";
                if ((int)duzhi.Length / (1024 * 1024) > 10)
                {
                    Response.Write("<script>alert('文件大于10M，请重新上传');</script>");
                    return;
                }
                //判断文件类型是否符合要求 
                for (int i = 0; i < restrictExtension.Length; i++)
                {
                    if (fileExtension == restrictExtension)
                    {
                        fileIsValid = true;

                    }

                }
                //如果文件类型符合要求,调用SaveAs方法实现上传,并显示相关信息 
                if (fileIsValid == true)
                {
                    try
                    {
                        this.FileUpload1.SaveAs(Server.MapPath("~/txt/") + FileUpload1.FileName);

                    }
                    catch
                    {

                    }

                }

            }
            else
            {
                Response.Write("<script>alert('只能够上传后缀为.txt的文件夹');</script>");

            }
            #endregion
            ExistsFile(Server.MapPath("txt/" + FileUpload1.FileName)); //检查文件是否存在
                                                                       //读取文件
            StreamReader sr = new StreamReader(Server.MapPath("txt/" + FileUpload1.FileName),
                System.Text.Encoding.Default);

            duzhi = sr.ReadToEnd();
            sr.Close();
            int count = 0;
            string erro = string.Empty;
            string[] arr = System.Text.RegularExpressions.Regex.Split(duzhi, "\r\n"); //回车截取
            string verif = @"^(\d{2}\s){5}\d{2}\+\d{2}$";   
            Regex ex = new Regex(verif);
            int aa = 0;

            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == "" && i != arr.Length - 1)
                {
                    continue;   //如果，当前文本等于空，结束
                }
                else
                {
                    if (i == arr.Length - 1)
                    {
                        zifucuan = zifucuan.Replace("+", "*");  //把，带有加号的替换成*
                        InputData += arr[i];
                        InputData = InputData.ToString().Replace(" ","*");  //把空格替换成*
                    }
                    else
                    {
                        InputData += arr[i] + ",";
                    }
                aa = aa + 1;
                zifucuan += arr[i] + ",";
                }
                

                if (arr[i].Replace(" ", "") != "")
                {

                    if (ex.IsMatch(arr[i]))
                    {
                        string[] spiltstr = arr[i].Replace("+", " ").Split(' ');

                        for (int j = 0; j < spiltstr.Length; j++)
                        {
                            if (j >= 6)
                            {
                                if (Convert.ToInt32(spiltstr[j]) >= 17)
                                {
                                    erro = erro + "第" + (i + 1).ToString() + "行,后一位数不能大于16";
                                }
                            }
                            else if (Convert.ToInt32(spiltstr[j]) >= 34)
                            {
                                erro = erro + "第" + (i + 1).ToString() + "行 ,前六位数不能大于33";
                            }
                        }

                        if (erro == "")
                        {
                            count++;
                            strtext = strtext + "第" + aa + "注" + arr[i] + "<br />";
                            bbb = aa + "";
                            if (i != arr.Length - 1)
                            {
                                h1.Visible = true;
                            }
                        }

                    }
                    else
                    {
                        if (arr[i] != "" && arr[i] != null)
                        {
                            erro = erro + "第" + (aa).ToString() + "行格式不正确";

                            break;
                        }
                    }
                }
            }
            if (erro != "")
            {
                strtext = "";
                Response.Write("<script>alert('很遗憾，号码上传失败:" + erro + "');</script>");
                return;

            }
            else
            {

                zhs += count;
                Response.Write("<script>alert('恭喜您，号码上传成功共:" + count + "注');</script>");
                note = count;

            }
        }

        else
        {
            Response.Write("<script>alert('请选择文件(以.txt结尾的文本文件)');</script>");

        }
        // 每周二、四、日21:15	Wednesday周三
        if (strWeek == "Tuesday" || strWeek == "Thursday" || strWeek == " Sunday")
        {
            if (strGetTime <= strFixTime)
            {
               

                if (zhs >= 20000)
                {

                    Response.Write("<script>alert('注数不能超过20000');</script>");

                    zhs = 0;
                }
                zhs = 0;

            }


        }

    }

 

}

